const KoaBouncer = require('koa-bouncer')
const {GETPACKAGE,ADDPACKAGE,UPDATEPACKAGE,DELPACKAGE,SEARCHPACKAGE} = require('../service/package.service')
module.exports = {
    // 获取套餐
    getPackage: async (ctx, next) => {
        await GETPACKAGE(ctx, next)
    },
    // 添加套餐
    addPackage: async (ctx, next) => {
        try {
            ctx.validateBody('id')
                .required('id必须得传值')
                .isString()
                .trim()
            ctx.validateBody('packageName')
                .required('packageName必须得传值')
                .isString()
                .trim()
                ctx.validateBody('packagePrice')
                .required('packagePrice必须得传值')
                .isString()
                .trim()
                ctx.validateBody('packageImg')
                .required('packageImg必须得传值')
                .isString()
                .trim()
                ctx.validateBody('packageNum')
                .required('packageNum必须得传值')
                .isString()
                .trim()
                await ADDPACKAGE(ctx, next)
        } catch (error) {
            if (error instanceof KoaBouncer.ValidationError) {
                ctx.body = {
                    code: 500,
                    msg: error.message
                }
                return
            } else {
                throw error;
            }
        }
       
    },
    // 更新套餐
    updatePackage: async (ctx, next) => {
        try {
            ctx.validateBody('id')
                .required('id必须得传值')
                .isString()
                .trim()
            ctx.validateBody('packageName')
                .required('packageName必须得传值')
                .isString()
                .trim()
                ctx.validateBody('packagePrice')
                .required('packagePrice必须得传值')
                .isString()
                .trim()
                ctx.validateBody('packageImg')
                .required('packageImg必须得传值')
                .isString()
                .trim()
                ctx.validateBody('packageNum')
                .required('packageNum必须得传值')
                .isString()
                .trim()
                await UPDATEPACKAGE(ctx, next)
        } catch (error) {
            if (error instanceof KoaBouncer.ValidationError) {
                ctx.body = {
                    code: 500,
                    msg: error.message
                }
                return
            } else {
                throw error;
            }
        }
      
    },
    // 删除套餐
    delPackage: async (ctx, next) => {
        try {
            ctx.validateBody('id')
                .required('id必须得传值')
                .isString()
                .trim()
                await DELPACKAGE(ctx, next)
        } catch (error) {
            if (error instanceof KoaBouncer.ValidationError) {
                ctx.body = {
                    code: 500,
                    msg: error.message
                }
                return
            } else {
                throw error;
            }
        }
      
    },
    // 搜索套餐
    searchPackage: async (ctx, next) => {
        try {
            ctx.validateBody('keyWord')
                .required('keyWord必须得传值')
                .isString()
                .trim()
                
            ctx.validateBody('belongToShop')
                .required('belongToShop必须得传值')
                await SEARCHPACKAGE(ctx, next)
        } catch (error) {
            if (error instanceof KoaBouncer.ValidationError) {
                ctx.body = {
                    code: 500,
                    msg: error.message
                }
                return
            } else {
                throw error;
            }
        }
      
    },
}